package guoanmaker.operator.business.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import guoanmaker.operator.business.model.OperatorDepartment;
import guoanmaker.operator.business.model.OperatorRole;
import guoanmaker.operator.business.model.OperatorUser;
import guoanmaker.operator.business.model.ReturnResult;
import guoanmaker.operator.business.repository.OperatorDepartmentRepository;
import guoanmaker.operator.business.repository.OperatorUserRepository;
import guoanmaker.operator.helper.common.util.StringUtil;

@Service
public class OperatorDepartmentService {
	
	private  final  OperatorDepartmentRepository  operatorDepartmentRepository;
	private  final  OperatorUserRepository  operatorUserRepository;
	
	@Autowired  
	private  HttpServletRequest request;
	
	
	@Autowired
	public OperatorDepartmentService(OperatorDepartmentRepository  operatorDepartmentRepository,OperatorUserRepository  operatorUserRepository){
		
		this.operatorDepartmentRepository=operatorDepartmentRepository;
		this.operatorUserRepository=operatorUserRepository;
	}
	
	
	public OperatorDepartmentRepository getOperatorDepartmentRepository(){
		
		return operatorDepartmentRepository;
	}
	
	public OperatorUserRepository getOperatorUserRepository(){
		
		return operatorUserRepository;
	}
	
	@Transactional
	public ReturnResult  addDepartment(String depName,String userid){
		ReturnResult returnResult=new ReturnResult();
		OperatorDepartment Department=operatorDepartmentRepository.findByDepName(depName);
		if(Department!=null){
			returnResult.setKey("error");
			returnResult.setValue("部门名称不可重复");
		}else{
			OperatorUser operatorUser =operatorUserRepository.findOne(userid);
			OperatorDepartment  operatorDepartment=new OperatorDepartment();
			operatorDepartment.setDepname(depName);
			operatorDepartment.setCreateDate(new Date());
			operatorDepartment.setStatus(1);
			if(operatorUser!=null){
				operatorDepartment.setCreateUserid(operatorUser.getId());
				operatorDepartment.setCreateUser(operatorUser.getUsername());
			}
			operatorDepartmentRepository.save(operatorDepartment);
			returnResult.setKey("success");
			returnResult.setValue("保存成功");
			
		}
		return returnResult;
	}
	
	
	@Transactional
	public ReturnResult deleteDps(String ids){
		ReturnResult returnResult=new ReturnResult();
			String[] str=StringUtil.StrList(ids);
			List<OperatorDepartment>  lists=new ArrayList<>();
			String id="";
			OperatorDepartment operatorDepartment=null;
			for (int i = 0; i < str.length; i++) {
				operatorDepartment=new OperatorDepartment();
				id=str[i];
				operatorDepartment.setId(id);
				lists.add(operatorDepartment);
			}
			operatorDepartmentRepository.delete(lists);
			returnResult.setKey("success");
			returnResult.setValue("删除成功");
		return returnResult;
	}
	
	@Transactional
	public ReturnResult deleteBydepOne(String id){
		ReturnResult returnResult=new ReturnResult();
			OperatorDepartment  operatorDepartment=operatorDepartmentRepository.findOne(id);
			if(operatorDepartment!=null){
				operatorDepartmentRepository.delete(id);
				returnResult.setKey("success");
				returnResult.setValue("删除成功");
		
		}
		return returnResult;
	}
	
	@Transactional
	public ReturnResult editDepartment(String id,String depname,String  userid){
		ReturnResult returnResult=new ReturnResult();
		OperatorDepartment Department=operatorDepartmentRepository.findByDepName(depname);
		if(Department!=null){
			if(!depname.equals("")&&(Department.getDepname()!=null&&!Department.getDepname().equals(""))){
				if(Department.getDepname().equals(depname)){
					returnResult.setKey("success");
					returnResult.setValue("编辑成功");
				}else{
					returnResult.setKey("error");
					returnResult.setValue("部门名称不可重复");
				}
			}
		}else{
			OperatorDepartment  operatorDepartment=operatorDepartmentRepository.findOne(id);
			OperatorUser operatorUser =operatorUserRepository.findOne(userid);
			operatorDepartment.setDepname(depname);
			operatorDepartment.setCreateDate(new Date());
			operatorDepartment.setStatus(1);
			if(operatorUser!=null){
				operatorDepartment.setCreateUserid(operatorUser.getId());
				operatorDepartment.setCreateUser(operatorUser.getUsername());
			}
			operatorDepartmentRepository.save(operatorDepartment);
			returnResult.setKey("success");
			returnResult.setValue("编辑成功");
		}
		return  returnResult;
	}
	
	public Page<OperatorDepartment>  queryAllDepsPage(Integer page,Integer size){
		Sort sort = new Sort(Sort.Direction.DESC, "id");
		Pageable pageable = new PageRequest(page, size, sort);
		Page<OperatorDepartment>  list=operatorDepartmentRepository.findAll(pageable);
		if(list.getContent().size()>0){
			for (OperatorDepartment operatorDepartment : list.getContent()) {
			  List<OperatorUser>  lists= 	operatorUserRepository.selectBydepId(operatorDepartment.getId());
			  if(lists.size()>0){
				  operatorDepartment.setStatus(0);
				  operatorDepartmentRepository.save(operatorDepartment);
			  }else{
				  operatorDepartment.setStatus(1);
				  operatorDepartmentRepository.save(operatorDepartment);
			  }
			}
		}
		return list;
	}
	public List<OperatorDepartment>  queryAllDeps(){
		List<OperatorDepartment>  list=operatorDepartmentRepository.findAll();
		return list;
	}
	
	public List<String>  queryAllDepsforAccounts(String id){
		List<String> lists=new ArrayList<>();
		List<OperatorUser> list=operatorUserRepository.selectBydepId(id);
		if(list.size()>0){
			for (OperatorUser operatorUser : list) {
				lists.add(operatorUser.getUsername());
			}
		}
		return lists;
	}
	

}
